<?php
/**
*
* @package phpBB3 Social Network
* @author  kmklr72
* @copyright (c) 2009 kmklr72
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

/**
* @ignore
*/
if (!defined('IN_PHPBB'))
{
	exit;
}

/*
*
*  The following code is from the Simple Profile Comments MOD by platinum_2007 (Ian Taylor)
*  All credits to him for this code
*
*/

$user_id = request_var('u', 0);

include($phpbb_root_path . 'includes/functions_posting.' . $phpEx);
generate_smilies('inline', 2);
$user->add_lang('posting');
$userid = (int) $user->data['user_id'];

// Change the comment counter back to 0 when the user visits his/her profile
if ($user_id == $userid)
{
	$timestamp = time();
	$sqlupd = 'UPDATE ' . USERS_TABLE . '
		SET	user_last_comment = ' . $timestamp . '
		WHERE user_id = ' . $userid;
	$db->sql_query($sqlupd);
}

$start	= request_var('comments', 0);
$limit	= $config['comm_per_page'];
$pagination_url = append_sid($phpbb_root_path . 'memberlist.' . $phpEx , 'mode=viewprofile&amp;u='.$user_id);

$sql = $db->sql_build_query('SELECT', array(
	'SELECT'	=> 'u.username, u.user_avatar, u.user_avatar_type, u.user_avatar_width, u.user_avatar_height, u.user_colour, u.allow_all_comment, c.*',
	'FROM'		=> array(
		USERS_TABLE		=> 'u',
	),
	'LEFT_JOIN'	=> array(
		array(
			'FROM'	=> array(SN_COMMENT_TABLE => 'c'),
			'ON'	=> 'u.user_id = c.poster_id'		
		)
	),
	'WHERE'		=> 'c.user_id = ' . $user_id,
	'ORDER_BY'	=> 'c.comment_id DESC'
));
$result = $db->sql_query_limit($sql, $limit, $start);

while ($row = $db->sql_fetchrow($result))
{
	$row['bbcode_options'] = (($row['enable_bbcode']) ? OPTION_FLAG_BBCODE : 0) +
		(($row['enable_smilies']) ? OPTION_FLAG_SMILIES : 0) + 
		(($row['enable_magic_url']) ? OPTION_FLAG_LINKS : 0);
	$comment_text_format = generate_text_for_display($row['message'], $row['bbcode_uid'], $row['bbcode_bitfield'], $row['bbcode_options']);
	$avatar 			= get_user_avatar($row['user_avatar'], $row['user_avatar_type'], $row['user_avatar_width'], $row['user_avatar_height']);
	$av_size 			= $config['sc_av_size'];
	$comment_id 		= $row['comment_id'];
	$comment_author_id 	= $row['poster_id'];

	// Assign the needed template vars.
	$template->assign_block_vars('comments', array(	
		'COMMENT_DATE'			=> $user->format_date($row['post_time']),
		'COMMENT_AUTHOR'		=> get_username_string('full', $row['poster_id'], $row['username'], $row['user_colour']),
		'COMMENT_TEXT'			=> $comment_text_format,
		'COMMENT_AVATAR_THUMB'	=> ($row['user_avatar']) ? get_user_avatar($row['user_avatar'], $row['user_avatar_type'], ($row['user_avatar_width'] > $row['user_avatar_height']) ? $av_size : ($av_size / $row['user_avatar_height']) * $row['user_avatar_width'], ($row['user_avatar_height'] > $row['user_avatar_width']) ? $av_size : ($av_size / $row['user_avatar_width']) * $row['user_avatar_height']) : '',
		'U_USER_URL'			=> append_sid("{$phpbb_root_path}memberlist.$phpEx", 'mode=viewprofile&amp;u='.$comment_author_id),
		'U_DELETE_URL'			=> append_sid("{$phpbb_root_path}sn_blocks/comments/new_comment.$phpEx", 'act=delete&amp;comment='.$comment_id.'&amp;u='.$comment_author_id.'&amp;p='.$user_id),	
		'CAN_DELETE'			=> ($user_id == $user->data['user_id'] || $user->data['user_id'] == $row['poster_id']) ? true : false,
	));
}
$db->sql_freeresult($result);			

// count some stuff up for the pagination
$sql = 'SELECT COUNT(comment_id) AS comment_number 
	FROM ' . SN_COMMENT_TABLE . ' 
	WHERE user_id = ' . $user_id;
$result = $db->sql_query($sql);
$total_comments = $db->sql_fetchfield('comment_number');
$db->sql_freeresult($result);

// Are you my foe?
$sql = 'SELECT friend, foe
	FROM ' . ZEBRA_TABLE . '
	WHERE zebra_id = ' . (int) $user->data['user_id'] . '
	AND user_id = ' . $user_id;
$result = $db->sql_query($sql);
$row = $db->sql_fetchrow($result);
$db->sql_freeresult($result);

// are you my friend?
$sql = 'SELECT friend, foe
	FROM ' . ZEBRA_TABLE . '
	WHERE zebra_id = ' . $user_id . '
	AND user_id = ' . (int) $user->data['user_id'];
$result = $db->sql_query($sql);
$row = $db->sql_fetchrow($result);
$db->sql_freeresult($result);

$user_ids = ($user_id == (int) $user->data['user_id']) ? true : false;

$sql = 'SELECT * FROM ' . USERS_TABLE . ' WHERE user_id = ' . $user_id;
$result = $db->sql_query($sql);
$member = $db->sql_fetchrow($result);
$db->sql_freeresult($result);

$template->assign_vars(array(
	'ALLOW_ALL_COMMENT'			=> ($member['allow_all_comment']) ? true : false,
	'ALLOW_FRIEND_ONLY'			=> ($member['allow_friend_only'] && !$row['friend']  && !$user_ids) ? true : false,
	'ALLOW_FRIEND_VIEW'			=> ($member['allow_friend_view'] && !$row['friend']  && !$user_ids) ? true : false,		
	'FOE'						=> ($row['foe']) ? true : false,
	'S_BBCODE_ALLOWED'			=> true,

	'COMMENT_TO_ID'				=> $user_id,		
	'C_PAGINATION'				=> $sn->paginate_this($pagination_url, $total_comments, $limit, $start, false, '', 'comments', $page_ary),
	'PAGE_NUMBER'				=> on_page($total_comments, $limit, $start),
	'NO_COMMENT'				=> $total_comments,
	'SIMPLE_COMMENT_ENABLED'	=> $config['enable_comment'],

	'S_COMMENT_FORM_ACTION'		=> append_sid("{$phpbb_root_path}sn_blocks/comments/new_comment.$phpEx", 'act=new_comment&amp;u='.$user_id),
));

?>